﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace Aribaccio.Copyright.Register
{
    public partial class MainWindowModel : ObservableModel
    {
        /// <summary>
        /// 是否已初始化加载
        /// </summary>
        protected bool IsLoaded { get; set; }
        /// <summary>
        /// 注册版权期限实例
        /// </summary>
        protected IRegisterCopyLimits CopyLimits { get; set; }

        /// <summary>
        /// 是否允许启用注册
        /// </summary>
        [ObservableProperty]
        protected bool isEnabledRegedit = true;
        /// <summary>
        /// 期限时间
        /// </summary>
        [ObservableProperty]
        protected DateTime limitDateTime;
        /// <summary>
        /// 机器码
        /// </summary>
        [ObservableProperty]
        protected string machineCode = string.Empty;
        /// <summary>
        /// 注册码
        /// </summary>
        [ObservableProperty]
        protected string regeditCode = string.Empty;
        /// <summary>
        /// 填充状态消息
        /// </summary>
        [ObservableProperty]
        protected string statusMsg = string.Empty;
        /// <summary>
        /// 填充状态画刷颜色
        /// </summary>
        [ObservableProperty]
        protected Brush statusForeground = new SolidColorBrush(Colors.White);

        /// <summary>
        /// 构造函数
        /// </summary>
        public MainWindowModel(IRegisterCopyLimits argRegisterCopyLimits)
        {
            this.CopyLimits = argRegisterCopyLimits;
            this.LimitDateTime = DateTime.Today.AddDays(30);
        }

        /// <summary>
        /// 生成注册码命令事件
        /// </summary>
        [RelayCommand]
        public void CreateRegedit()
        {
            if (!this.IsEnabledRegedit) return;
            try
            {
                this.IsEnabledRegedit = false;
                if (this.LimitDateTime == default || this.LimitDateTime <= DateTime.Today)
                {
                    this.SetStatusMsg("期限日期必须大于当前系统日期，请重新选择！", Colors.Red);
                    return;
                }
                if (string.IsNullOrWhiteSpace(this.MachineCode))
                {
                    this.SetStatusMsg("机器码不能为空，请提供！", Colors.Red);
                    return;
                }
                //生成注册码序列号
                string resultSerialNum = CopyLimits.CreateSerialNum(this.MachineCode.Trim(), this.LimitDateTime);
                //输出到界面
                this.RegeditCode = resultSerialNum;
                if (!string.IsNullOrWhiteSpace(resultSerialNum))
                    this.SetStatusMsg("注册码生成成功！", Colors.Green);
                else
                    this.SetStatusMsg("注册码生成失败！", Colors.Red);
            }
            finally
            {
                this.IsEnabledRegedit = true;
            }
        }

        /// <summary>
        /// 复制注册码命令事件
        /// </summary>
        [RelayCommand]
        public void ClipboardRegedit()
        {
            if (string.IsNullOrWhiteSpace(this.RegeditCode))
            {
                this.SetStatusMsg("注册码为空，请先生成注册码", Colors.Red);
                return;
            }
            Clipboard.SetText(this.RegeditCode.Trim());
            this.SetStatusMsg("注册码复制成功", Colors.Green);
        }

        /// <summary>
        /// 设置状态信息
        /// </summary>
        protected void SetStatusMsg(string argMsg, Color argColor)
        {
            this.StatusMsg = argMsg;
            this.StatusForeground = new SolidColorBrush(argColor);
        }
    }
}
